﻿using System;
using Microsoft.Win32;

namespace SourceBackup
{
    enum VSVersion
    {
        VS2005 = 1,
        VS2008 = 2,
        VS2010 = 3,
        VS2012 = 4,
        VS2013 = 5,
        VS2012EFD = 6,
        VS2013EFD = 7,
        VS2015 = 8,
    }
    class VSExternalTools
    {
        private static string VS2005Key = "Software\\Microsoft\\VisualStudio\\8.0\\External Tools";
        private static string VS2008Key = "Software\\Microsoft\\VisualStudio\\9.0\\External Tools";
        private static string VS2010Key = "Software\\Microsoft\\VisualStudio\\10.0\\External Tools";
        private static string VS2012Key = "Software\\Microsoft\\VisualStudio\\11.0\\External Tools";
        private static string VS2013Key = "Software\\Microsoft\\VisualStudio\\12.0\\External Tools";
        private static string VS2015Key = "Software\\Microsoft\\VisualStudio\\14.0\\External Tools";
        private static string VS2012ExpressForDesktopKey = "Software\\Microsoft\\WDExpress\\11.0\\External Tools";
        private static string VS2013ExpressForDesktopKey = "Software\\Microsoft\\WDExpress\\12.0\\External Tools";
        private VSExternalTools() { }

        public static void SetTool(string toolTitle, string toolCmd, string toolArg, string initialDir, int toolOpt)
        {
            SetTool(toolTitle, toolCmd, toolArg, initialDir, toolOpt, VSVersion.VS2010);
        }
		public static void SetTool(string toolTitle, string toolCmd, string toolArg, string initialDir, int toolOpt, VSVersion vsVersion) 
		{
			int toolCount;
			int toolId = 0;
			RegistryKey key = null;
            string vsKey;
            switch (vsVersion)
            {
                case VSVersion.VS2005:
                    vsKey = VS2005Key;
                    break;
                case VSVersion.VS2008:
                    vsKey = VS2008Key;
                    break;
                case VSVersion.VS2010:
                    vsKey = VS2010Key;
                    break;
                case VSVersion.VS2012:
                    vsKey = VS2012Key;
                    break;
                case VSVersion.VS2013:
                    vsKey = VS2013Key;
                    break;
                case VSVersion.VS2015:
                    vsKey = VS2015Key;
                    break;
                case VSVersion.VS2012EFD:
                    vsKey = VS2012ExpressForDesktopKey;
                    break;
                case VSVersion.VS2013EFD:
                    vsKey = VS2013ExpressForDesktopKey;
                    break;
                default:
                    vsKey = VS2010Key;
                    break;
            }
			try 
			{
                key = Registry.CurrentUser.OpenSubKey(vsKey, true);
				if (key != null) 
				{
					toolId = ToolExists(key, toolTitle);
					if (toolId == -1) //Adding tool
					{
						toolCount = (int)key.GetValue("ToolNumKeys");
						key.SetValue("ToolTitle" + toolCount.ToString(), toolTitle);
						key.SetValue("ToolCmd" + toolCount.ToString(), toolCmd.Replace("\"",""));
						key.SetValue("ToolArg" + toolCount.ToString(), toolArg);
						key.SetValue("ToolDir" + toolCount.ToString(), initialDir);
						key.SetValue("ToolSourceKey" + toolCount.ToString(), "");
						key.SetValue("ToolOpt" + toolCount.ToString(), toolOpt);
						key.SetValue("ToolNumKeys", toolCount + 1);
					}
					else //Updating tool
					{
						key.SetValue("ToolCmd" + toolId.ToString(), toolCmd.Replace("\"",""));
						key.SetValue("ToolArg" + toolId.ToString(), toolArg);
						key.SetValue("ToolDir" + toolId.ToString(), initialDir);
						key.SetValue("ToolOpt" + toolId.ToString(), toolOpt);
					}
					key.Flush();
				}
			}
			finally 
			{
				if (key != null)
					key.Close();
			}
		}

		private static int ToolExists(RegistryKey key, string toolTitle) 
		{
			int toolCount;
			string keyValue;
			toolCount = (int)key.GetValue("ToolNumKeys");
			try 
			{
				for (int i = 0; i <= toolCount; i++) 
				{
					keyValue = key.GetValue("ToolTitle" + i.ToString()).ToString();
					if (keyValue.ToUpper() == toolTitle.ToUpper())
						return i;
				}
			}
			catch {}
			return -1;
		}

        public static bool IsVSInstalled(VSVersion vsVersion)
        {
            bool isInstalled = false;
            string vsKey;
            switch (vsVersion)
            {
                case VSVersion.VS2005:
                    vsKey = VS2005Key;
                    break;
                case VSVersion.VS2008:
                    vsKey = VS2008Key;
                    break;
                case VSVersion.VS2010:
                    vsKey = VS2010Key;
                    break;
                case VSVersion.VS2012:
                    vsKey = VS2012Key;
                    break;
                case VSVersion.VS2013:
                    vsKey = VS2013Key;
                    break;
                case VSVersion.VS2015:
                    vsKey = VS2015Key;
                    break;
                case VSVersion.VS2012EFD:
                    vsKey = VS2012ExpressForDesktopKey;
                    break;
                case VSVersion.VS2013EFD:
                    vsKey = VS2013ExpressForDesktopKey;
                    break;
                default:
                    vsKey = VS2010Key;
                    break;
            }
            RegistryKey key = null;
            try
            {
                key = Registry.CurrentUser.OpenSubKey(vsKey);
                if (key != null)
                {
                    isInstalled = true;
                }
            }
            finally
            {
                if (key != null)
                    key.Close();
            }
            return isInstalled;
        }
        public static bool IsToolRegistered(string toolTitle, string toolCmd)
        {
            return IsToolRegistered(toolTitle, toolCmd, VSVersion.VS2010);
        }
        public static bool IsToolRegistered(string toolTitle, string toolCmd, VSVersion vsVersion) 
		{
			bool isRegistered = false;
			int toolId = 0;
            string vsKey;
            switch (vsVersion)
            {
                case VSVersion.VS2005:
                    vsKey = VS2005Key;
                    break;
                case VSVersion.VS2008:
                    vsKey = VS2008Key;
                    break;
                case VSVersion.VS2010:
                    vsKey = VS2010Key;
                    break;
                case VSVersion.VS2012:
                    vsKey = VS2012Key;
                    break;
                case VSVersion.VS2013:
                    vsKey = VS2013Key;
                    break;
                 case VSVersion.VS2015:
                    vsKey = VS2015Key;
                    break;
                case VSVersion.VS2012EFD:
                    vsKey = VS2012ExpressForDesktopKey;
                    break;
                case VSVersion.VS2013EFD:
                    vsKey = VS2013ExpressForDesktopKey;
                    break;
                default:
                    vsKey = VS2010Key;
                    break;
            }
            //= vsVersion == VSVersion.VS2010 ? VS2010Key : vsVersion == VSVersion.VS2008 ? VS2008Key : VS2005Key;
			RegistryKey key = null;
			string registeredToolCmd;
			try 
			{
                key = Registry.CurrentUser.OpenSubKey(vsKey);
				if (key != null) 
				{
					toolId = ToolExists(key, toolTitle);
					if (toolId > -1)
					{
                       
						registeredToolCmd = key.GetValue("ToolCmd" + toolId.ToString()).ToString();
						if (registeredToolCmd == toolCmd)
						{
							isRegistered = true;
						}
					}
				}
			}
			finally 
			{
				if (key != null)
					key.Close();
			}
			return isRegistered;
		}
    }
}
